package com.kaiwukj.android.ufamily.mvp.ui.page.home.message.add;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.StringUtils;
import com.kaiwukj.android.mcas.di.component.AppComponent;
import com.kaiwukj.android.ufamily.R;
import com.kaiwukj.android.ufamily.app.base.review.BaseMvpActivity;
import com.kaiwukj.android.ufamily.app.constant.ARouterUrlKt;
import com.kaiwukj.android.ufamily.di.component.DaggerMessageComponent;
import com.kaiwukj.android.ufamily.di.module.MessageModule;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.ChatSearchGroupResult;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.ChatSearchPersonResult;
import com.kaiwukj.android.ufamily.mvp.ui.page.message.MessageContract;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

@Route(path = ARouterUrlKt.MessageSearchUrl)
public class FriendAddActivity extends BaseMvpActivity<FriendAddPresenter> implements MessageContract.SearchView {

    @BindView(R.id.et_search)
    EditText etSearch;
    @BindView(R.id.tv_cancel)
    TextView tvCancel;
    @BindView(R.id.container_notice)
    ViewGroup containerNotice;
    @BindView(R.id.tv_find_person)
    TextView tvFindPerson;
    @BindView(R.id.tv_find_group)
    TextView tvFindGroup;
    @BindView(R.id.container_person_result)
    ViewGroup containerSearchResult;
    @BindView(R.id.tv_result_title)
    TextView tvResultTitle;
    @BindView(R.id.rv_list)
    RecyclerView rvList;

    private PersonResultAdapter mPersonAdapter;//搜人
    private GroupResultAdapter mGroupAdapter;//搜话题

    private int eventPos = -1;


    @OnClick(R.id.tv_cancel)
    void onCancelClick() {
        onBackPressedSupport();
    }

    @OnClick({R.id.container_person, R.id.container_group})
    void onSearchTypeClick(View view) {
        String keyword = etSearch.getText().toString().trim();
        if (StringUtils.isEmpty(keyword)) {
            showMessage("搜索内容不能为空");
            return;
        }
        switch (view.getId()) {
            case R.id.container_person:
                mPresenter.searchPerson(keyword);
                break;
            case R.id.container_group:
                mPresenter.searchGroup(keyword);
                break;
        }
    }

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        super.setupActivityComponent(appComponent);
        DaggerMessageComponent.builder()
                .appComponent(appComponent)
                .messageModule(new MessageModule(this))
                .build().inject(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_friend_add;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        mPersonAdapter = new PersonResultAdapter();

    /*    mPersonAdapter.setOnItemClickListener((adapter, view, position) ->
                ARouter.getInstance().build(ARouterUrlKt.DynamicPersonUrl)
                        .withInt("userId", mPersonAdapter.getItem(position).getUserId())
                        .navigation());*/

        mPersonAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            switch (view.getId()) {
                case R.id.btn_attention:
                    boolean isConcern = mPersonAdapter.getItem(position).isConcern();
                    if (isConcern) {
                        showMessageOKCancel(this, "是否不再关注TA", v ->
                        {
                            eventPos = position;
                            mPresenter.postCancelAttention(mPersonAdapter.getItem(position).getUserId());
                        });
                    } else {
                        eventPos = position;
                        mPresenter.postAttention(mPersonAdapter.getItem(position).getUserId());
                    }
                    break;
                case R.id.iv_avatar:
                    ARouter.getInstance().build(ARouterUrlKt.DynamicPersonUrl)
                            .withInt("userId", mPersonAdapter.getItem(position).getUserId())
                            .navigation();
                    break;
            }
        });

        mGroupAdapter = new GroupResultAdapter();

        rvList.setLayoutManager(new LinearLayoutManager(this));

        etSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (count > 0) {
                    containerSearchResult.setVisibility(View.GONE);
                    containerNotice.setVisibility(View.VISIBLE);
                    tvFindPerson.setText(String.format("找人: %s", s.toString()));
                    tvFindGroup.setText(String.format("话题: %s", s.toString()));
                } else {
                    containerNotice.setVisibility(View.GONE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        mGroupAdapter.setOnItemClickListener((adapter, view, position) ->
                ARouter.getInstance().build(ARouterUrlKt.TopicIndexUrl)
                        .withInt("topicId", (int) adapter.getItemId(position))
                        .navigation()
        );

    }

    @Override
    protected void initData(Bundle savedInstanceState) {

    }

    @Override
    public void onPersonResult(List<ChatSearchPersonResult> results) {
        tvResultTitle.setText("查找的人");
        mPersonAdapter.setList(results);
        rvList.setAdapter(mPersonAdapter);
        containerSearchResult.setVisibility(View.VISIBLE);
        containerNotice.setVisibility(View.GONE);
    }

    @Override
    public void onGroupResult(List<ChatSearchGroupResult> results) {
        tvResultTitle.setText("查找的话题");
        mGroupAdapter.setList(results);
        rvList.setAdapter(mGroupAdapter);
        containerSearchResult.setVisibility(View.VISIBLE);
        containerNotice.setVisibility(View.GONE);
    }

    @Override
    public void onTaskSuccess(int taskId) {
        super.onTaskSuccess(taskId);
        switch (taskId) {
            case FriendAddPresenter.ATTENTION:
                if (eventPos != -1) {
                    mPersonAdapter.postAttention(eventPos);
                    eventPos = -1;
                }
                break;
            case FriendAddPresenter.ATTENTION_CANCEL:
                if (eventPos != -1) {
                    mPersonAdapter.postCancelAttention(eventPos);
                    eventPos = -1;
                }
                break;
        }
    }
}
